package jiyun.com.yiyanmeng;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.allenliu.badgeview.BadgeFactory;
import com.allenliu.badgeview.BadgeView;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import base.BaseObserver;
import bean.LunTanSchoolNewsBean;
import bean.PinLunBean;
import bean.ResultBean;
import jiyun.com.yiyanmeng.http.HttpManager;
import jiyun.com.yiyanmeng.http.MySerivce;
import okhttp3.ResponseBody;
import utils.ProgressDialogs;
import utils.RxUtil;
import utils.ToastUtil;

public class SheQuSchoolDetailActivity extends AppCompatActivity implements View.OnClickListener{

    /**
     * 返回
     */
    private TextView mImageBack;
    /**
     * 详情
     */
    private TextView mTvTitle;
    private TextView mTvRight;
    private RelativeLayout mRl;
    private FrameLayout mRlMain;
    private WebView mWebView;
    private View mLine;
    /**
     * 评论...
     */
    private TextView mTvComment;
    private ImageView mIvPingNum;
    private String ping_num;
    /**
     * 评论
     */
    private TextView mTxtPingNum;
    private LinearLayout mLl;
    private ImageView mIvCollect;
    private RelativeLayout mRlBottom;
    private BottomSheetDialog dialog;
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shequ_school_detail);
        Intent intent = getIntent();
        id=intent.getIntExtra("id",0);
        Log.i("position", "onCreate: "+id+"");
        initView();
    }

    private void initView() {
        mImageBack = (TextView) findViewById(R.id.image_back);
        mImageBack.setOnClickListener(this);
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvRight = (TextView) findViewById(R.id.tv_right);
        mRl = (RelativeLayout) findViewById(R.id.rl);
        mRlMain = (FrameLayout) findViewById(R.id.rl_main);
        mWebView = (WebView) findViewById(R.id.webView);
        mLine = (View) findViewById(R.id.line);
        mTvComment = (TextView) findViewById(R.id.tv_comment);
        mTvComment.setOnClickListener(this);
        mIvPingNum = (ImageView) findViewById(R.id.iv_ping_num);
        mTxtPingNum = (TextView) findViewById(R.id.txt_ping_num);
        mLl = (LinearLayout) findViewById(R.id.ll);
        mLl.setOnClickListener(this);
        mIvCollect = (ImageView) findViewById(R.id.iv_collect);
        mIvCollect.setOnClickListener(this);
        mRlBottom = (RelativeLayout) findViewById(R.id.rl_bottom);
        initData();
    }

    //web
    protected void initData() {
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.loadUrl("https://www.baidu.com/");
    }

    //点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.image_back:
                //关闭本页面
                finish();
                break;
            case R.id.tv_comment:
                //弹出评论框,并评论
                showCommentDialog();
                break;
            case R.id.iv_collect:
                //收藏
                break;
            case R.id.ll:
                //点击跳评论详情
                Intent intent = new Intent(SheQuSchoolDetailActivity.this, SchoolPostCommentListActivity.class);
                intent.putExtra("id",id);
                startActivity(intent);
                break;
        }
    }

    //弹出评论框,并评论1
    private void showCommentDialog() {
        //弹出pop,新控件
        dialog = new BottomSheetDialog(this);
        View view = LayoutInflater.from(this).inflate(R.layout.layout_comment_dialog, null);
        final EditText commentText = (EditText) view.findViewById(R.id.edit_content);
        final TextView bt_comment = (TextView) view.findViewById(R.id.tv_finish);
        dialog.setContentView(view);
        /**
         * 解决bsd显示不全的情况
         */
        View parent = (View) view.getParent();
        BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
        view.measure(0,0);
        behavior.setPeekHeight(view.getMeasuredHeight());
        dialog.show();
        //点击输入评论事件
        bt_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String comment = commentText.getText().toString().trim();
                Log.i("tag", "评论内容: "+comment);
                if (!TextUtils.isEmpty(comment)){
                    //发表评论
                    submit(comment);
                }else{
                    Toast.makeText(SheQuSchoolDetailActivity.this,"评论内容不能为空",Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    //发表评论2
    private void submit(String comment) {
        //自定义ProgressDialog
        ProgressDialogs.showProgressDialog(this);
        //数据请求去提交
        Log.i("tag", "id: "+id);
        MySerivce serivce = HttpManager.getInstance().getSerivce(MySerivce.base_URL, MySerivce.class);
        serivce.pinbean(""+id,comment)
                .compose(RxUtil.<PinLunBean>changeThread())
                .subscribe(new BaseObserver <PinLunBean>() {
                    @Override
                    public void onSuccess(PinLunBean pinLunBean) {
                        Log.i("tag", "onSuccess: "+pinLunBean.getRet());

                        if (pinLunBean.getRet().equals(200)){
                            Log.i("pinLunBean", "onSuccess: "+pinLunBean.getInfo().toString());

                            BadgeFactory.create(SheQuSchoolDetailActivity.this)
                                    .setTextColor(Color.WHITE)
                                    .setWidthAndHeight(15,15)
                                    .setBadgeBackground(Color.RED)
                                    .setTextSize(10)
                                    .setBadgeGravity(Gravity.RIGHT|Gravity.TOP)
                                    .setBadgeCount(Integer.parseInt(ping_num)+1)
                                    .setShape(BadgeView.SHAPE_CIRCLE)
                                    .setSpace(5,0)
                                    .bind(mIvPingNum);
                            ToastUtil.show(SheQuSchoolDetailActivity.this,pinLunBean.getMas());

                        }else{
                            ToastUtil.show(SheQuSchoolDetailActivity.this,pinLunBean.getMas());
                        }
                        ProgressDialogs.dismissDialog();
                        dialog.dismiss();

                    }

                    @Override
                    public void onFail(String error) {


                    }
                });

    }


}
